//
//  NewsViewController.m
//  Conference
//
//  Created by 陈鑫 on 12-11-16.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//会议新闻

#import "NewsViewController.h"
#import "MainDataBase.h"
#import "NewsData.h"
#import "NewsCellController.h"
#import "NewsDetailViewController.h"
#import "NetContant.h"
#import "WebData.h"
#import "Globals.h"
#import "Constant.h"
#import "DateTool.h"
@interface NewsViewController ()

@end

@implementation NewsViewController
@synthesize table;
@synthesize arrayOfData;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad

{

    [self loadingData];
    
    [super viewDidLoad];
    
}
-(void)loadingData{
    [self getDataFromDB];
    NSThread *thread=[[[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] autorelease];
    [thread start];
    
}

-(void)getDataFromDB{
    if(self.arrayOfData!=nil||[self.arrayOfData count]>0)
        [self.arrayOfData removeAllObjects];
    self.arrayOfData=[[MainDataBase getInstance]getNewsDatas];
}
-(void)run{
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    NSString *sRet=[[WebData getInstance] getNews];
    
    [self performSelectorOnMainThread:@selector(onPostExecute:) withObject:sRet waitUntilDone:YES];
    
	[pool release];
    
}

-(void)onPostExecute:(NSString *)sRet{
    
    if ([sRet isEqualToString:[[NetContant getInstance] NET_CONNECT_FALSE]]) {
        
        
        // [[Globals getInstance] showAlertView:[[Constant getInstance] net_error]];
        
        
    }else if([sRet isEqualToString:[[NetContant getInstance] NETWORK_RECORD_ISEMPTY]]){
        
        //   [[Globals getInstance] showAlertView:[[Constant getInstance] account_error]];
        
    }else {
        @try {
            [self doNetRet:sRet];
            [self doNetInfo];
        }
        @catch (NSException *exception) {
            
        }
        
        
    }
    
}
-(void)doNetRet:(NSString *)sRet{
    NSArray *tables=[sRet componentsSeparatedByString:[[NetContant getInstance]TABLES_SPLIT_FLAG]];
    NSMutableArray *dataList=[NSMutableArray arrayWithCapacity:0];
    for (int i=0; i<[tables count]; i++) {
        if(nil!=[tables objectAtIndex:i]){
            NSArray *record=[[tables objectAtIndex:i]componentsSeparatedByString:[[NetContant getInstance] RECORDS_SPLIT_FLAG]];
            if([record count]>=6){
                NewsData *newsData=[[NewsData alloc]init];
                newsData.ID=[record objectAtIndex:0];
                newsData.title = [record objectAtIndex:1];
                newsData.message = [record objectAtIndex:2];
                newsData.writer=[record objectAtIndex:3];
                newsData.dateTime=[record objectAtIndex:4];
                newsData.arrImg=[record objectAtIndex:5];
                
                [dataList addObject:newsData];
                [newsData release];
            }
        }
    }
    [[MainDataBase getInstance]addNews:dataList];
    [Globals getInstance].sNewsUpdate=[NSString stringWithFormat:@"%@ %@",[[DateTool getInstance]getNowDate],[[DateTool getInstance]getNowTime]];
    
}

-(void)doNetInfo{
    [self getDataFromDB];
    [self.table reloadData];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return newCellHeight;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.arrayOfData count];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *newsCell=@"newsCell";
    NewsCellController *cell=(NewsCellController *)[tableView dequeueReusableCellWithIdentifier:newsCell];
    if(cell ==nil){
		NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"NewsCellController" owner:self options:nil];   
		for (id oneObject in array) {
			if ([oneObject isKindOfClass:[NewsCellController class]]) {
				cell=oneObject;
				break;
			}
		}
        
        cell.selectionStyle=UITableViewCellSelectionStyleBlue;
      //  cell.backgroundView=[[[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"msg_bg_normal.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:20]] autorelease];
        
      //   cell.selectedBackgroundView=[[[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"msg_bg_pressed.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:20]] autorelease];
        
    }
    NewsData *data=[self.arrayOfData objectAtIndex:[indexPath row]];
    cell.lab_title.text=data.title;
    cell.lab_time.text=data.dateTime;
    cell.lab_content.text=data.message;

    return cell;
    
}

-(void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsDetailViewController *mNewsDetailViewController=[[NewsDetailViewController alloc]initWithNibName:@"NewsDetailViewController" bundle:nil];
    mNewsDetailViewController.newsData=[self.arrayOfData objectAtIndex:[indexPath row]];
    [self.navigationController pushViewController:mNewsDetailViewController animated:YES];
    [mNewsDetailViewController release];
    [tableView1 deselectRowAtIndexPath:indexPath animated:YES];
    
}
- (void)viewDidUnload
{
    [self setArrayOfData:nil];
    [self setTable:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [arrayOfData release];
    [table release];
    [super dealloc];
}
@end
